home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 5 / BBS in a Box -Volume V (BBS in a Box) (April 1992).iso / Files / Util / F-Fi / Find Entity v1.0b.cpt / Show_help.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-30  |  27.4 KB  |  1,018 lines  |  [TEXT/KAHL]

  1. #define A4_PROJECT    0    /* Set to 1 for a code resource (DA, cdev, etc.) */
  2. #define USE_PICTS    1    /* 1 if PICTs are to be displayed, 0 for text only */
  3. #define ONE_RESOURCE 1    /* 1 for Get1Resource, 0 for GetResource, etc. */
  4.  
  5. /*
  6.     Show_help by James W. Walker, June 1991
  7.     This code is freely usable.
  8.     
  9.     e-mail:
  10.         Internet            76367.2271@compuserve.com
  11.         CIS                    76367,2271
  12.         America Online        JWWalker
  13.     
  14.     This code displays scrolling text in a dialog box.  The text comes
  15.     from TEXT/styl resources, which can be created with ResEdit 2.1.
  16.     Pictures can be included in the text using the same scheme as
  17.     TeachText: Each option-space character indicates where the top
  18.     edge of a picture should go, and pictures are centered horizontally.
  19.     Pictures come from consecutively-numbered PICT resources.  The text
  20.     cannot be edited, but one can select text and copy it to the
  21.     clipboard using command-C, or save it as a TeachText file.
  22.     
  23.     The fact that our window is not resizeable has its advantages.  We
  24.     can compute where the pictures go, once and for all, except to take
  25.     vertical scrolling into account.
  26.     
  27.     A popup menu can be used to jump to "bookmarks", which are indicated
  28.     by tab characters at ends of lines.
  29.     
  30.     Prototype:
  31.     
  32.     void Show_help( short help_dlog_id, short help_text_id,
  33.         short base_pict_id, StringPtr default_filename,
  34.         StringPtr default_menuname );
  35.     
  36.     help_dlog_id  is the resource ID of the DLOG resource.  The dialog
  37.                   should have an OK button as item 1 and a userItem, to
  38.                   display the text, in item 2.
  39.     
  40.     help_text_id is the resource ID of the TEXT and styl resources.
  41.     
  42.     base_pict_id is the resource ID of the first PICT resource.
  43.  
  44.     default_filename is the filename initially presented in the Save dialog
  45.                   when the user saves the help text as TeachText.
  46.     
  47.     default_menuname is the menu item used for a line that consists of
  48.                   a tab and nothing else.
  49. */
  50.  
  51.  
  52. #ifndef NIL
  53. #define        NIL        0L
  54. #endif
  55.  
  56. #if A4_PROJECT
  57. #include <SetUpA4.h>
  58. #endif
  59.  
  60. #if ONE_RESOURCE
  61. #define GetResource            Get1Resource
  62. #define CountResources        Count1Resources
  63. #endif
  64.  
  65. #define CHECKPOINTS 0
  66. #define ASSERTIONS    0
  67.  
  68. #if ASSERTIONS
  69. #define        ASSERT(x,y)        if (!(x)) {DebugStr("\p" y)}
  70. #else
  71. #define        ASSERT(x,y)
  72. #endif
  73.  
  74. #if CHECKPOINTS
  75. #define CKPT(x)        DebugStr( "\p" x );
  76. #else
  77. #define CKPT(x)
  78. #endif
  79.  
  80. enum {
  81.     c_OK = 1,    /* OK button */
  82.     c_help,        /* userItem for our help display */
  83.     c_save,        /* Button to save as TeachText */
  84.     c_menu        /* userItem for popup menu */
  85. };
  86.  
  87. #define        SCROLLBAR_WIDTH    16
  88. #define        TEXT_INSET        4
  89.  
  90. typedef struct {
  91.     Rect        bounds;
  92.     PicHandle    pict;
  93. } pict_info;
  94.  
  95. typedef struct {
  96.     DialogRecord    dialog;
  97.     ControlHandle    scrollbar;
  98. #if USE_PICTS
  99.     Boolean            high_defer_flag;
  100.     Rect            high_rect[3];
  101.     short            high_waiting;
  102.     short            pict_count;    /* how many pictures */
  103.     pict_info        *pict_data;    /* pointer to an array */
  104. #endif
  105. }    help_record, *help_ptr;
  106.  
  107. /* private global variables */
  108. static    RgnHandle    save_clip;
  109. static    CursHandle    watch_cursor, ibeam_cursor;
  110.  
  111. /* Prototypes of public routines */
  112. void Show_help( short help_dlog_id, short help_text_id,
  113.     short base_pict_id, StringPtr default_filename,
  114.     StringPtr default_menuname );
  115. void Flash_button( DialogPtr the_dialog, short item_number );
  116.  
  117. /* Prototypes of private routines
  118.  */
  119. static pascal Boolean Help_filter( DialogPtr dialog,
  120.     EventRecord    *event, short *itemHit);
  121. static pascal void  userItem_proc( WindowPtr the_window, short item_num );
  122. static pascal void  Menu_userItem_proc( WindowPtr the_window, short item_num );
  123. static pascal void Scroll_text( ControlHandle the_bar, int part_code );
  124. static pascal Auto_scroll( void );
  125. static void Handle_scroll( DialogPtr dialog,
  126.     short the_part, Point local_point );
  127. static void Adjust_text( DialogPtr dialog );
  128. static void Save_text( TEHandle the_text, short base_pict_id,
  129.     StringPtr default_filename );
  130. static void Topic_menu( DialogPtr dlog, MenuHandle help_popup );
  131. static MenuHandle Build_popup( TEHandle    the_text, StringPtr default_menuname );
  132.  
  133. #if USE_PICTS
  134. static void Find_pictures( DialogPtr dlog, short first_pict_id );
  135. static void Draw_picts( WindowPtr the_window, Rect *update_rect );
  136. static pascal void High_hook( Rect *high_rect );
  137. static void High_hook_glue( void );
  138. static void Do_deferred_hilites( help_ptr  hptr, Rect *update_rect );
  139. #endif
  140.  
  141. /* ------------------------- Show_help --------------------------------- */
  142. void Show_help( short help_dlog_id, short help_text_id,
  143.     short base_pict_id, StringPtr default_filename,
  144.     StringPtr default_menuname )
  145. {
  146.     register    DialogPtr    dptr;
  147.     register    TEHandle    the_text;
  148.     short        itype, ihit;
  149.     Handle        item_h;
  150.     Rect        help_item_box, box;
  151.     Handle            help_TEXT;
  152.     StScrpHandle    help_styl;
  153.     GrafPtr        save_port;
  154.     Rect        dest, view;
  155.     ControlHandle    the_bar;
  156.     short        max_scroll, nLines;
  157.     Point        place;
  158.     MenuHandle    help_popup;
  159.     
  160.     watch_cursor = GetCursor( watchCursor );
  161.     SetCursor( *watch_cursor );
  162.     ibeam_cursor = GetCursor( iBeamCursor );
  163.     dptr = (DialogPtr) NewPtr( sizeof(help_record) );
  164.     dptr = GetNewDialog( help_dlog_id, (DialogPeek) dptr, (WindowPtr)-1L );
  165.     ASSERT( dptr != NIL, "Failed GetNewDialog" );
  166.     GetPort( &save_port );
  167.     SetPort( dptr );
  168.     
  169.     help_TEXT = GetResource( 'TEXT', help_text_id );
  170.     if (help_TEXT == NIL)
  171.     {
  172.         ASSERT(false, "Failed to find help TEXT resource" );
  173.         SysBeep(1);
  174.         goto getout;
  175.     }
  176.     help_styl = (StScrpHandle) GetResource( 'styl', help_text_id );
  177.     if (help_styl == NIL)
  178.     {
  179.         DisposHandle( help_TEXT );
  180.         ASSERT( false, "Failed to find styl resource" );
  181.         SysBeep(1);
  182.         goto getout;
  183.     }
  184.     HLock( help_TEXT );
  185.     
  186.     GetDItem( dptr, c_help, &itype, &item_h, &help_item_box );
  187.     SetDItem( dptr, c_help, itype, (Handle) userItem_proc, &help_item_box );
  188.     view = help_item_box;
  189.     InsetRect( &view, 1, 1 );
  190.     view.right -= SCROLLBAR_WIDTH;
  191.     dest = view;
  192.     InsetRect( &dest, TEXT_INSET, 0 );
  193.     the_text = TEStylNew( &dest, &view );
  194.     ASSERT( the_text != NIL, "Failed TEStylNew." );
  195.     
  196.     TEStylInsert( *help_TEXT, GetHandleSize(help_TEXT),
  197.         help_styl, the_text );
  198.     TECalText( the_text );    /* Maybe not needed, but can't hurt. */
  199.     TEActivate( the_text );
  200.     ReleaseResource( (Handle) help_styl );
  201.     ReleaseResource( help_TEXT );
  202.     nLines = (**the_text).nLines;
  203.     SetWRefCon( dptr, (long)the_text );
  204.     max_scroll = TEGetHeight( (long) nLines, 1L, the_text )
  205.         - (view.bottom - view.top);
  206.     
  207.     help_item_box.left = help_item_box.right - SCROLLBAR_WIDTH;
  208.     the_bar = NewControl( dptr, &help_item_box, "\p", true,
  209.         0, 0, max_scroll, scrollBarProc, NIL );
  210.     ASSERT( the_bar != NIL, "Failed NewControl for scroll bar." );
  211.     ((help_ptr) dptr)->scrollbar = the_bar;
  212.  
  213. #if USE_PICTS
  214.     Find_pictures( dptr, base_pict_id );
  215. #endif
  216.     help_popup = Build_popup( the_text, default_menuname );
  217.  
  218.     TEAutoView( TRUE, the_text );    /* Permit auto-scrolling */
  219.     CKPT( "Installing ClikLoop" );
  220.     (**the_text).clikLoop = (ProcPtr)Auto_scroll;
  221. #if USE_PICTS
  222.     (**the_text).highHook = (ProcPtr) High_hook_glue;
  223.     ((help_ptr)dptr)->high_defer_flag = false;
  224. #endif
  225.  
  226.     GetDItem( dptr, c_menu, &itype, &item_h, &box );
  227.     SetDItem( dptr, c_menu, itype, (Handle) Menu_userItem_proc, &box );
  228.     
  229.     save_clip = NewRgn();    /* Used in Draw_picts */
  230.     ShowWindow( dptr );
  231. #if A4_PROJECT
  232.     RememberA4();
  233. #endif
  234.     InitCursor();
  235.  
  236.     do {
  237.         ModalDialog( (ProcPtr) Help_filter, &ihit );
  238.         if (ihit == c_save)
  239.             Save_text( the_text, base_pict_id, default_filename );
  240.         else if (ihit == c_menu)
  241.             Topic_menu( dptr, help_popup );
  242.     } while (ihit != c_OK);
  243.     
  244.     
  245.     DisposeRgn( save_clip );
  246. #if USE_PICTS
  247.     DisposPtr( (Ptr) ((help_ptr)dptr)->pict_data );
  248. #endif
  249.     TEDispose( the_text );
  250. getout:
  251.     DisposDialog( dptr );
  252.     DisposeMenu( help_popup );
  253.     SetPort( save_port );
  254. }
  255.  
  256. /* --------------------------- Build_popup ------------------------- */
  257. /*
  258.     Build a popup menu of the sections of the help text.  We scan for
  259.     tab characters.  The text between the tab character and the preceding
  260.     line break will be a menu item, unless it is the null string; then we
  261.     use the default menu name that was passed to Show_help.
  262. */
  263. static MenuHandle Build_popup( TEHandle    the_text, StringPtr default_menuname )
  264. {
  265.     MenuHandle    popup;
  266.     short        menu_id;
  267.     SignedByte    text_state;
  268.     Handle        text_h;    /* handle to just the text */
  269.     Str255        menu_data;
  270.     char        *text;    /* pointer to the help text */
  271.     register short        scan, line_start;
  272.     short        text_size, title_length;
  273.     
  274.     /* Find an unused menu ID */
  275.     menu_id = 1300; /* no particular reason */
  276.     while (GetMHandle(menu_id))
  277.         ++menu_id;
  278.     
  279.     popup = NewMenu( menu_id, "\p" );
  280.  
  281.     text_h = (**the_text).hText;
  282.     text_state = HGetState( text_h );
  283.     HLock( text_h );
  284.     text = *text_h;
  285.     text_size = (short) GetHandleSize( text_h );
  286.     line_start = 0;
  287.     for (scan = 0; scan < text_size; scan++ )
  288.     {
  289.         if (text[scan] == '\r')
  290.         {
  291.             line_start = scan + 1;
  292.         }
  293.         else if (text[scan] == '\t')
  294.         {
  295.             title_length = scan - line_start;
  296.             if (title_length == 0)
  297.                 AppendMenu( popup, default_menuname );
  298.             else if (title_length <= 255)
  299.             {
  300.                 menu_data[0] = title_length;
  301.                 BlockMove( &text[line_start], &menu_data[1], title_length );
  302.                 AppendMenu( popup, menu_data );
  303.             }
  304.         }
  305.     }
  306.     
  307.     HSetState( text_h, text_state );
  308.     return popup;
  309. }
  310.  
  311. /* ------------------------- Topic_menu ------------------------ */
  312. /*
  313.     This routine is called when the menu title is clicked.
  314.     It pops up the menu and scrolls to the indicated tab character.
  315. */
  316. static void Topic_menu( DialogPtr dptr, MenuHandle menu )
  317. {
  318.     short            menu_id;
  319.     Handle            item_h;
  320.     short            itype;
  321.     Rect            box;
  322.     Point            where;
  323.     long            menu_return;
  324.     short            menu_choice;
  325.     ControlHandle    bar;
  326.     char            tab_char[2] = "\t";
  327.     register short        i;
  328.     register TEHandle    the_text;
  329.     Handle            text_h;
  330.     SignedByte        text_state;
  331.     register long        offset;
  332.     TextStyle        what_style;
  333.     short            line_height, font_ascent;
  334.     
  335.     if (menu == NIL) return;
  336.     InsertMenu( menu, -1 );
  337.     GetDItem( dptr, c_menu, &itype, &item_h, &box );
  338.     where.h = box.left;
  339.     where.v = box.bottom;
  340.     LocalToGlobal( &where );
  341.     InvertRect( &box );
  342.     menu_return = PopUpMenuSelect( menu, where.v, where.h, 0 );
  343.     InvertRect( &box );
  344.     if (HiWord(menu_return))    /* Something selected */
  345.     {
  346.         menu_choice = LoWord( menu_return );
  347.         bar = ((help_ptr)dptr)->scrollbar;
  348.         the_text = (TEHandle) GetWRefCon( dptr );
  349.         text_h = (**the_text).hText;
  350.         text_state = HGetState( text_h );
  351.         HLock( text_h );
  352.         
  353.         /* Find tab character number menu_choice */
  354.         offset = -1L;
  355.         for (i = 1; i <= menu_choice; ++i)
  356.         {
  357.             ++offset; /* so we don't find the same thing twice */
  358.             offset = Munger(text_h, offset, tab_char, 1L, NIL, NIL);
  359.         }
  360.  
  361.         *(long *)&where = TEGetPoint( (short)offset, the_text );
  362.         TEGetStyle( (short)offset, &what_style, &line_height,
  363.             &font_ascent, the_text );
  364.         where.v -= line_height;    /* align to TOP of tab */
  365.         /*
  366.             Now where.v is in local coordinates.
  367.         */
  368.         where.v -= (**the_text).destRect.top;
  369.         SetCtlValue( bar,  where.v );
  370.         HSetState( text_h, text_state );
  371.         
  372.         Adjust_text( dptr );
  373.     }
  374.     menu_id = (**menu).menuID;
  375.     DeleteMenu( menu_id );
  376. }
  377.  
  378. /* ------------------------- Save_text ------------------------ */
  379. /*
  380.     This is called when the user clicks on the "Save as TeachText"
  381.     button.
  382. */
  383. static void Save_text( TEHandle the_text, short base_pict_id,
  384.     StringPtr default_filename )
  385. {
  386.     Point    where;
  387.     static Str255    prompt = "\pName of TeachText file:";
  388.     SFReply        reply;
  389.     OSErr        err;
  390.     short        data_refnum, res_refnum, old_resfile;
  391.     Handle        text_data;
  392.     SignedByte    state;
  393.     long        count;
  394. #if USE_PICTS
  395.     register short        num_picts, pict_id;
  396.     Handle        old_pict, new_pict;
  397. #endif
  398.     
  399.     where.h = where.v = 100;
  400.     SFPutFile( where, prompt, default_filename, NIL, &reply );
  401.     if (reply.good)
  402.     {
  403.         old_resfile = CurResFile();
  404. #if USE_PICTS
  405.         num_picts = CountResources( 'PICT' );
  406. #endif
  407.         
  408.         err = FSDelete( reply.fName, reply.vRefNum );
  409.         err = Create( reply.fName, reply.vRefNum, 'ttxt', 'ttro' );
  410.         err = FSOpen( reply.fName, reply.vRefNum, &data_refnum );
  411.         text_data = (**the_text).hText;
  412.         state = HGetState(text_data );
  413.         HLock( text_data );
  414.         count = GetHandleSize( text_data );
  415.         err = FSWrite( data_refnum, &count, *text_data );
  416.         err = FSClose( data_refnum );
  417.         HSetState( text_data, state );
  418.  
  419. #if USE_PICTS
  420.         if (num_picts > 0)
  421.         {
  422.             err = SetVol( NIL, reply.vRefNum );
  423.             CreateResFile( reply.fName );
  424.             err = ResError();
  425.             res_refnum = OpenResFile( reply.fName );
  426.             err = ResError();
  427.             for (pict_id = base_pict_id;
  428.                 pict_id < base_pict_id + num_picts; ++pict_id )
  429.             {
  430.                 UseResFile( old_resfile );
  431.                 old_pict = GetResource( 'PICT', pict_id );
  432.                 if (old_pict == NIL)
  433.                     break;
  434.                 new_pict = old_pict;
  435.                 err = HandToHand( &new_pict );
  436.                 UseResFile( res_refnum );
  437.                 AddResource( new_pict, 'PICT',
  438.                     pict_id - base_pict_id + 1000, "\p" );
  439.                 err = ResError();
  440.             }
  441.             CloseResFile( res_refnum );
  442.             err = FlushVol( NIL, reply.vRefNum );
  443.         }
  444. #endif
  445.     }
  446. }
  447.  
  448. #if USE_PICTS
  449. /* ------------------------- High_hook_glue ------------------------ */
  450. static void High_hook_glue( void )
  451. {
  452.     asm {
  453.         move.L    (SP)+, A0    ; get address of rectangle
  454.         movem.L    A2-A5/D3-D7, -(SP)    ; save registers
  455.         move.L    A0, -(SP)
  456.     }
  457.     CKPT( "High_hook_glue" );
  458.     asm {
  459.         JSR        High_hook
  460.         movem.L    (SP)+, A2-A5/D3-D7    ; restore registers
  461.         RTS
  462.     }
  463. }
  464.  
  465. /* ------------------------- High_hook -------------------------- */
  466. /*
  467.     This deferred highlighting scheme is used to ensure that highlighting
  468.     will be done after any pictures have been drawn, not before.  To do
  469.     otherwise can cause pictures to be incorrectly highlighted during
  470.     auto-scrolling.  This effect can be seen in TeachText.
  471. */
  472. static pascal void High_hook( Rect *the_rect )
  473. {
  474.     register help_ptr    front;
  475.     
  476.     CKPT( "High_hook");
  477.     front = (help_ptr) FrontWindow();
  478.     if ( (front == NIL) ||
  479.         (GetPtrSize((Ptr)front) != sizeof(help_record)) )
  480.         return;
  481.     if (!front->high_defer_flag)
  482.         InvertRect( the_rect );
  483.     else
  484.     {
  485.         if ( front->high_waiting >= 3 )
  486.             DebugStr("\pHighlight overflow");
  487.         else
  488.         {
  489.             front->high_rect[ front->high_waiting ]
  490.                 = *the_rect;
  491.             front->high_waiting++;
  492.         }
  493.     }
  494. }
  495. #endif /* USE_PICTS */
  496.  
  497. /* ------------------------- Auto_scroll ----------------------------- */
  498. /*
  499.     This is a ClikLoop routine, called repeatedly by TEClick when the
  500.     mouse is down.
  501. */
  502. static pascal Auto_scroll()
  503. {
  504.     register    WindowPtr    the_display;
  505.     register    ControlHandle    the_bar;
  506.     Point                    mouse_point;
  507.     Rect                    view_rect;
  508.     register     TEHandle    the_text;
  509.     
  510.     asm {
  511.         movem.l        a1-a5/d1-d7, -(SP)
  512.     }
  513.     CKPT( "Auto_scroll");
  514.     the_display = FrontWindow();
  515.     if ( (the_display != NIL) &&
  516.         (GetPtrSize((Ptr)the_display) == sizeof(help_record)) )
  517.     {
  518.         the_text = (TEHandle) GetWRefCon( the_display );
  519.         the_bar = ((help_ptr) the_display)->scrollbar;
  520.         
  521.         GetMouse( &mouse_point );
  522.         view_rect = (**the_text).viewRect;
  523.         if (mouse_point.v < view_rect.top)
  524.             Scroll_text( the_bar, inUpButton );
  525.         else if (mouse_point.v > view_rect.bottom)
  526.             Scroll_text( the_bar, inDownButton );
  527.     }
  528.     asm {
  529.         movem.L        (SP)+, a1-a5/d1-d7
  530.         moveQ        #1, D0
  531.     }
  532. }
  533.  
  534. #if USE_PICTS
  535. /* ------------------------- Draw_picts --------------------------------- */
  536. /*
  537.     Called by Adjust_text and userItem_proc to draw pictures.
  538. */
  539. static void Draw_picts( WindowPtr the_window, Rect *update_rect )
  540. {
  541.     register TEHandle    the_text;
  542.     register short        pict_count, pict_index;
  543.     PicHandle    the_pict;
  544.     short         v_offset;
  545.     Rect        pict_loc, dummy;
  546.     
  547.     CKPT( "Draw_picts");
  548.     the_text = (TEHandle) GetWRefCon( the_window );
  549.     v_offset = (**the_text).destRect.top - (**the_text).viewRect.top
  550.         - TEXT_INSET;
  551.     pict_count = ((help_ptr) the_window)->pict_count;
  552.     for (pict_index = 0; pict_index < pict_count; pict_index++)
  553.     {
  554.         pict_loc = ((help_ptr) the_window)->pict_data[pict_index].bounds;
  555.         OffsetRect( &pict_loc, 0, v_offset );
  556.         if (!SectRect( &pict_loc, update_rect, &dummy ))
  557.             continue;
  558.         the_pict = ((help_ptr) the_window)->pict_data[pict_index].pict;
  559.         LoadResource( (Handle) the_pict );
  560.         HLock( (Handle) the_pict );
  561.         GetClip( save_clip );
  562.         ClipRect( update_rect );
  563.         DrawPicture( the_pict, &pict_loc );
  564.         SetClip( save_clip );
  565.         HUnlock( (Handle) the_pict );
  566.     }
  567. }
  568.  
  569. /* ---------------------- Find_pictures ---------------------------- */
  570. static void Find_pictures( DialogPtr dlog, short first_pict_id )
  571. {
  572.     register TEHandle    the_text;
  573.     Handle        text_h;
  574.     SignedByte    text_state;
  575.     register long        offset;
  576.     long        text_size;
  577.     short        num_picts;
  578.     register short        which_pict;
  579.     char        option_space[2] = "\xCA";
  580.     pict_info    *pict;
  581.     Point        place;
  582.     short        line_height, font_ascent;
  583.     TextStyle    what_style;
  584.     
  585.     CKPT( "Find_pictures");
  586.     the_text = (TEHandle) GetWRefCon( dlog );
  587.     text_h = (**the_text).hText;
  588.     text_state = HGetState( text_h );
  589.     HLock( text_h );
  590.     
  591.     /* Count option-space characters in the text. */
  592.     text_size = GetHandleSize( text_h );
  593.     offset = 0L;
  594.     num_picts = 0;
  595.     offset = Munger(text_h, offset, option_space, 1L, NIL, NIL );
  596.     while ( (offset >= 0L) && (offset <= text_size) )
  597.     {
  598.         num_picts++;
  599.         offset++;
  600.         offset = Munger(text_h, offset, option_space, 1L, NIL, NIL );
  601.         
  602.     }
  603.     
  604.     /* Allocate storage for an array of picture bounds. */
  605.     pict = (pict_info *) NewPtr( sizeof(pict_info) * num_picts );
  606.     ((help_ptr)dlog)->pict_data = pict;
  607.     
  608.     /*
  609.         Initialize the picture info.  For each picture we record the
  610.         picture handle and its rectangle, in unscrolled window
  611.         coordinates.
  612.     */
  613.     offset = 0L;
  614.     for (which_pict = 0; which_pict < num_picts; which_pict++)
  615.     {
  616.         pict[which_pict].pict = (PicHandle) GetResource( 'PICT',
  617.             first_pict_id + which_pict );
  618.         if ( pict[which_pict].pict == NIL )
  619.             break;
  620.         offset = Munger(text_h, offset, option_space, 1L, NIL, NIL );
  621.         *(long *)&place = TEGetPoint( (short)offset, the_text );
  622.         TEGetStyle( offset, &what_style, &line_height,
  623.             &font_ascent, the_text );
  624.         place.v -= line_height;    /* align picture with TOP of option-space */
  625.         offset++;
  626.         pict[which_pict].bounds = (**pict[which_pict].pict).picFrame;
  627.         OffsetRect( &pict[which_pict].bounds,
  628.             ( ((**the_text).destRect.right + (**the_text).destRect.left) -
  629.             (pict[which_pict].bounds.right + pict[which_pict].bounds.left)
  630.             ) / 2,
  631.             - pict[which_pict].bounds.top + place.v );
  632.     }
  633.     ((help_ptr)dlog)->pict_count = which_pict;
  634.     
  635. getout:
  636.     HSetState( text_h, text_state );
  637. }
  638. #endif /* USE_PICTS */
  639.  
  640. /* ---------------------- Scroll_text ---------------------------- */
  641. /*
  642.     This is used as a TrackControl actionProc for scrolling, and also
  643.     called by Auto_scroll for automatic scrolling.
  644. */
  645. static pascal void Scroll_text( ControlHandle the_bar, int part_code )
  646. {
  647.     register TEHandle    the_text;
  648.     register short        delta;
  649.     register WindowPtr    the_display;
  650.     short                old_value;
  651.     short                offset, line;
  652.     Point                place;
  653.     Rect                view;
  654.     TextStyle            style;
  655.     short                line_height, font_ascent;
  656.     
  657.     CKPT( "Scroll_text");
  658. #if A4_PROJECT
  659.     SetUpA4();
  660. #endif
  661.     if (part_code != 0)
  662.     {
  663.         the_display = (**the_bar).contrlOwner;
  664.         the_text = (TEHandle) GetWRefCon( the_display );
  665.         view = (**the_text).viewRect;
  666.         place.h = view.left + TEXT_INSET;
  667.         
  668.         switch (part_code)
  669.         {
  670.             case inUpButton:
  671.                 place.v = view.top - 4;
  672.                 offset = TEGetOffset( place, the_text );
  673.                 *(long *)&place = TEGetPoint( offset, the_text );
  674.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  675.                     the_text );
  676.                 delta = place.v - line_height - view.top;
  677.                 break;
  678.             case inDownButton:
  679.                 place.v = view.bottom + 2;
  680.                 offset = TEGetOffset( place, the_text );
  681.                 *(long *)&place = TEGetPoint( offset, the_text );
  682.                 /* Now place.v is at the baseline of the border line. */
  683.                 delta = place.v - view.bottom;
  684.                 break;
  685.             case inPageUp:
  686.                 /*
  687.                     I want top border line to remain visible, and
  688.                     the top of a line should end up at view.top.
  689.                 */
  690.                 place.v = view.top + 2;
  691.                 offset = TEGetOffset( place, the_text );
  692.                 *(long *)&place = TEGetPoint( offset, the_text );
  693.                 /* place.v is at the baseline of the top border line. */
  694.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  695.                     the_text );
  696.                 place.v += line_height - font_ascent;
  697.                 place.v -= view.bottom - view.top;
  698.                 offset = TEGetOffset( place, the_text );
  699.                 *(long *)&place = TEGetPoint( offset, the_text );
  700.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  701.                     the_text );
  702.                 delta = place.v - view.top;
  703.                 if (offset == 0)
  704.                     delta -= line_height;
  705.                 break;
  706.             case inPageDown:
  707.                 /*
  708.                     I want bottom border line to remain visible, and
  709.                     the bottom of a line should end up at view.bottom.
  710.                 */
  711.                 place.v = view.bottom - 2;
  712.                 offset = TEGetOffset( place, the_text );
  713.                 *(long *)&place = TEGetPoint( offset, the_text );
  714.                 /* place.v is at the baseline of the bottom border line. */
  715.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  716.                     the_text );
  717.                 place.v -= font_ascent; /* Top edge of bottom border line */
  718.                 place.v += view.bottom - view.top;
  719.                 /* We're looking at the bottom border of the next page. */
  720.                 offset = TEGetOffset( place, the_text );
  721.                 *(long *)&place = TEGetPoint( offset, the_text );
  722.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  723.                     the_text );
  724.                 delta =  place.v - line_height - view.bottom;
  725.                 if (offset == (**the_text).teLength)
  726.                     delta += line_height;
  727.                 break;
  728.         }
  729.         old_value = GetCtlValue( the_bar );
  730.         if ( ((delta < 0) && (old_value > 0)) ||
  731.             ((delta > 0) && (old_value < GetCtlMax(the_bar))) )
  732.         {
  733.             /*
  734.                 When this routine is called, TextEdit may have set the
  735.                 clipping region to the view rectangle, so we reset it
  736.                 here to make sure the scroll bar gets drawn.
  737.             */
  738.             GetClip( save_clip );
  739.             ClipRect( &the_display->portRect );
  740.             SetCtlValue( the_bar, old_value + delta );
  741.             SetClip( save_clip );
  742.         }
  743.         Adjust_text( the_display );
  744.     }
  745. #if A4_PROJECT
  746.     RestoreA4();
  747. #endif
  748. }
  749.  
  750. /* ---------------------- Adjust_text ---------------------------- */
  751. /*
  752.     Called by Handle_scroll and Scroll_text to scroll the text and
  753.     pictures into sync with the scroll bar's control value.
  754. */
  755. static void Adjust_text( DialogPtr    dialog )
  756. {
  757.     register    TEHandle    the_text;
  758.     register    short    scroll_down;
  759.     short            old_scroll;
  760.     Rect            update_rect;
  761.     ControlHandle    the_bar;
  762.     
  763.     CKPT( "Adjust_text");
  764.     the_text = (TEHandle) GetWRefCon( dialog );
  765.     the_bar = ((help_ptr) dialog)->scrollbar;
  766.     old_scroll = (**the_text).viewRect.top - (**the_text).destRect.top;
  767.     scroll_down = old_scroll - GetCtlValue( the_bar );
  768.     if (scroll_down == 0)
  769.         return;
  770. #if USE_PICTS
  771.     ((help_ptr) dialog)->high_defer_flag = true;
  772.     ((help_ptr) dialog)->high_waiting = 0;
  773. #endif
  774.     TEScroll( 0, scroll_down, the_text );
  775. #if USE_PICTS
  776.     update_rect = (**the_text).viewRect;
  777.     if (scroll_down > 0)
  778.     {
  779.         if (scroll_down < (update_rect.bottom - update_rect.top))
  780.             update_rect.bottom = update_rect.top + scroll_down;
  781.     }
  782.     else
  783.         if (- scroll_down < (update_rect.bottom - update_rect.top))
  784.             update_rect.top = update_rect.bottom + scroll_down;
  785.     Draw_picts( dialog, &update_rect );
  786.     Do_deferred_hilites( (help_ptr) dialog, &update_rect );
  787. #endif
  788. }
  789.  
  790. #if USE_PICTS
  791. /* ---------------------- Do_deferred_hilites ---------------------- */
  792. static void Do_deferred_hilites( help_ptr  hptr, Rect *update_rect )
  793. {
  794.     Rect            *hilite;
  795.     
  796.     while (hptr->high_waiting > 0)
  797.     {
  798.         hptr->high_waiting--;
  799.         hilite =
  800.             &hptr->high_rect[hptr->high_waiting];
  801.         if (SectRect( hilite, update_rect, hilite ))
  802.             InvertRect( hilite );
  803.     }
  804.     hptr->high_defer_flag = false;
  805. }
  806. #endif
  807.  
  808. /* ---------------------- Handle_scroll ---------------------------- */
  809. /*
  810.     Called by Help_filter to handle mouseDown events in the scroll bar.
  811. */
  812. static void Handle_scroll( DialogPtr dialog, short the_part, Point where )
  813. {
  814.     register    ControlHandle the_bar;
  815.     
  816.     CKPT( "Handle_scroll"); SetPort( dialog );
  817.     the_bar = ((help_ptr) dialog)->scrollbar;
  818.     if (the_part == inThumb)
  819.     {
  820.         (void) TrackControl( the_bar, where, NIL );
  821.         Adjust_text( dialog );
  822.     }
  823.     else
  824.         (void) TrackControl( the_bar, where, (ProcPtr)Scroll_text );
  825.     
  826. }
  827.  
  828. /* ---------------------- Help_filter ------------------------- */
  829. /*
  830.     This is the dialog event filter for our help window.
  831. */
  832. #define RETURN_KEY    0x24
  833. #define ENTER_KEY    0x4C
  834. #define TILDE_KEY    0x32
  835. #define ESCAPE_KEY    0x35
  836.  
  837.  
  838. static pascal Boolean Help_filter( DialogPtr dialog,
  839.     EventRecord    *event, short *itemHit)
  840. {
  841.     Point    local_point;
  842.     short    the_part;
  843.     ControlHandle    the_control;
  844.     short    keycode, charcode;
  845.     char    the_char;
  846.     register TEHandle    the_text;
  847.     Rect    item_box;
  848.     short    cursor;
  849.     
  850. #if A4_PROJECT
  851.     SetUpA4();
  852. #endif
  853.     the_text = (TEHandle) GetWRefCon( dialog );
  854.     GetMouse( &local_point );
  855.     if (PtInRect( local_point, &(**the_text).viewRect ))
  856.         SetCursor( *ibeam_cursor );
  857.     else
  858.         InitCursor();
  859.     TEIdle( the_text );
  860.     switch (event->what) {
  861.         case nullEvent:
  862.             break;
  863.         case mouseDown:
  864.             CKPT( "Help_filter mousedown");
  865.             local_point = event->where;
  866.             GlobalToLocal( &local_point );
  867.             the_part = FindControl( local_point, dialog, &the_control );
  868.             if (the_part && ((**the_control).contrlMax > 1) )
  869.             {
  870.                 Handle_scroll( dialog, the_part, local_point );    
  871.                 *itemHit = 2;
  872.                 break;
  873.             }
  874.             if (PtInRect( local_point, &(**the_text).viewRect ))
  875.             {
  876.                 if (event->modifiers & shiftKey)
  877.                     TEClick( local_point, true, the_text );
  878.                 else
  879.                     TEClick( local_point, false, the_text );
  880.             }
  881.             break;
  882.         case keyDown :    
  883.         case autoKey :
  884.             keycode = (event->message & keyCodeMask) >> 8;
  885.             charcode = event->message & charCodeMask;
  886.             /*
  887.                 There's no Cancel button, so we treat the OK button
  888.                 the same as a Cancel button.
  889.             */
  890.             if ( (keycode == RETURN_KEY) || (keycode == ENTER_KEY) ||
  891.                 (keycode == TILDE_KEY) || (keycode == ESCAPE_KEY) ||
  892.                 ((charcode == '.') && (event->modifiers & cmdKey)) )
  893.             {
  894.                 *itemHit = c_OK;    /* OK */
  895.                 Flash_button( dialog, *itemHit );
  896. #if A4_PROJECT
  897.                 RestoreA4();
  898. #endif
  899.                 return( TRUE );
  900.             }
  901.             if ( (charcode == 'c') && (event->modifiers & cmdKey) )
  902.             {
  903.                 (void) ZeroScrap();
  904.                 TECopy( the_text );
  905.                 SystemEdit(3);
  906.                 local_point = (**the_text).selPoint;
  907.                 *(long *)&local_point = PinRect( &(**the_text).viewRect,
  908.                     local_point );
  909.                 cursor = TEGetOffset( local_point, the_text );
  910.                 TESetSelect( (long)cursor, (long)cursor, the_text );
  911.                 event->what = nullEvent;
  912.             }
  913.             break;
  914.     } /* end switch */
  915.     
  916.     /* tell the Dialog Manager that the event has NOT been handled and that it should
  917.     ** take further action on this event.
  918.     */
  919. #if A4_PROJECT
  920.     RestoreA4();
  921. #endif
  922.     return false;
  923. }
  924.  
  925.  
  926. /* ---------------------- userItem_proc ------------------------- */
  927. static pascal void  userItem_proc( WindowPtr the_window, short item_num )
  928. {
  929.     Handle        item_h;
  930.     Rect        item_box;
  931.     short        item_type;
  932.     TEHandle    the_text;
  933.     
  934.     asm {
  935.         MOVEM.L    a1-a5/d0-d7, -(SP)
  936.     }
  937.     
  938.     CKPT( "UserItem_proc");
  939. #if A4_PROJECT
  940.     SetUpA4();
  941. #endif
  942.     the_text = (TEHandle) GetWRefCon( the_window );
  943.     item_box = (**the_text).viewRect;
  944. #if USE_PICTS
  945.     ((help_ptr) the_window)->high_defer_flag = true;
  946.     ((help_ptr) the_window)->high_waiting = 0;
  947. #endif
  948.     TEUpdate( &item_box, the_text );
  949.     
  950. #if USE_PICTS
  951.     Draw_picts( the_window, &item_box );
  952.     Do_deferred_hilites( (help_ptr) the_window, &item_box );
  953. #endif
  954.  
  955.     /*
  956.         Get the item's rectangle, and frame it.
  957.     */
  958.     GetDItem( the_window, item_num, &item_type, &item_h, &item_box );
  959.     FrameRect( &item_box );
  960.  
  961. #if A4_PROJECT
  962.     RestoreA4();
  963. #endif
  964.     asm {
  965.         movem.l    (SP)+, a1-a5/d0-d7        ; restore registers
  966.     }
  967. }
  968.  
  969. /* ---------------------- Menu_userItem_proc ------------------------- */
  970. static pascal void  Menu_userItem_proc( WindowPtr the_window, short item_num )
  971. {
  972.     Handle        item_h;
  973.     Rect        item_box;
  974.     short        item_type;
  975.     
  976.     asm {
  977.         MOVEM.L    a1-a5/d0-d7, -(SP)
  978.     }
  979.     
  980.     CKPT( "Menu_UserItem_proc");
  981. #if A4_PROJECT
  982.     SetUpA4();
  983. #endif
  984.     
  985.     /*
  986.         Get the item's rectangle, and frame it.
  987.     */
  988.     GetDItem( the_window, item_num, &item_type, &item_h, &item_box );
  989.     InsetRect(&item_box, -1, -1);
  990.     FrameRect( &item_box );
  991.     
  992.     /* Draw the drop-shadow */
  993.     MoveTo( item_box.left + 3, item_box.bottom );
  994.     LineTo( item_box.right, item_box.bottom );
  995.     LineTo( item_box.right, item_box.top + 3 );        
  996.  
  997. #if A4_PROJECT
  998.     RestoreA4();
  999. #endif
  1000.     asm {
  1001.         movem.l    (SP)+, a1-a5/d0-d7        ; restore registers
  1002.     }
  1003. }
  1004.  
  1005. /* ---------------------- Flash_button ----------------------------- */
  1006. void Flash_button( DialogPtr the_dialog, short item_number )
  1007. {
  1008.     ControlHandle    item_h;
  1009.     long    time;
  1010.     short    itype;
  1011.     Rect    box;
  1012.     
  1013.     GetDItem( the_dialog, item_number, &itype, (Handle *)&item_h, &box );
  1014.     HiliteControl( item_h, inButton );
  1015.     Delay( 9L, &time );
  1016.     HiliteControl( item_h, 0 );
  1017. }
  1018.